package com.ruoyi.mall.app.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mall.app.service.DtsCateDetailService;
import com.ruoyi.mall.app.service.DtsGoodsService;
import com.ruoyi.mall.app.service.DtsKeywordService;
import com.ruoyi.mall.common.domain.*;
import com.ruoyi.mall.common.mapper.DtsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/index/goods")
public class DtsGoodsController extends BaseController {

    @Autowired
    private DtsGoodsService dtsGoodsService;

    @Autowired
    private DtsCateDetailService dtscateDetailService;

    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;

    @GetMapping("/product")
    public AjaxResult product(DtsGoods dtsGoods)
    {
        if (dtsGoods == null || dtsGoods.getIsNew() == null) {
            startPage();
        } else {
            // 确保 getIsNew() 不为 null 后再进行比较
            if (dtsGoods.getIsNew() != 1) {
                startPage();
            }
        }
        List<DtsGoods> list = dtsGoodsService.selectGoodsNew(dtsGoods);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/popular")
    public AjaxResult popular(DtsGoods dtsGoods)
    {
        if (dtsGoods == null || dtsGoods.getIsHot() == null) {
            startPage();
        } else {
            // 确保 getIsHot() 不为 null 后再进行比较
            if (dtsGoods.getIsHot() != 1) {
                startPage();
            }
        }
        List<DtsGoods> list = dtsGoodsService.selectGoodsHot(dtsGoods);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/goodsDetail")
    public AjaxResult goodsDetail(@RequestParam(required = true) Long id)
    {
        if (id == null) {
            return error();
        }
        DtsGoods list = dtsGoodsService.selectGoodsById(id);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/goodsAttribute")
    public AjaxResult goodsAttribute(Long id)
    {
        if (id == null) {
            return error();
        }
        List<DtsGoodsAttribute> list = dtsGoodsService.selectGoodsAttribute(id);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/cateDetailGoodsList")
    public AjaxResult cateDetailGoodsList(Long id)
    {
        List<DtsGoods> list = dtsGoodsService.selectGoodsByCateId(id);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/search")
    public AjaxResult search(DtsGoods dtsGoods, long orderPrice)
    {
        List<DtsGoods> list = dtsGoodsService.selectGoodsByDtsGoods(dtsGoods,orderPrice);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/es/search")
    public AjaxResult searchGoodsEs(@RequestParam("name") String name,@RequestParam("orderBy") Integer orderBy, @RequestParam("categoryId") String categoryId) {
        List<DtsGoodsEs> result = dtsGoodsService.searchGoodsEs(name,orderBy);
        // 使用counterPrice字段值进行排序
        if (orderBy == 1) { // 按价格从低到高排序
            result.sort(Comparator.comparing(DtsGoodsEs::getCounterPrice));
        } else if (orderBy == 2) {  // 按价格从高到低排序
            result.sort((o1, o2) -> o2.getCounterPrice().compareTo(o1.getCounterPrice()));
        }
        if (categoryId != null && !categoryId.isEmpty() && !"0".equals(categoryId)) {
            // 进行分类的查询以及筛选
            List<DtsGoodsEs> filteredResult = new ArrayList<>();
            for (DtsGoodsEs goods : result) {
                if (goods.getCategoryId().toString().equals(categoryId)) {
                    filteredResult.add(goods);
                }
            }
            if (!filteredResult.isEmpty()) {
                return success(filteredResult);
            }
        }
        if (result != null && !result.isEmpty()) {
            return success(result);
        } else {
            return error("未找到相关商品");
        }
    }

    @GetMapping("/category")
    public AjaxResult category(DtsCategory dtsCategory)
    {
        List<DtsCategory> list = dtsCategoryMapper.selectCategory(dtsCategory);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/es/category")
    public AjaxResult searchCategory(@RequestParam("name") String name) {
        List<DtsGoodsEs> result = dtsGoodsService.searchGoodsEs(name, 0);

        // 提取唯一 categoryId 并转换为 DtsCategoryEs 列表
        List<DtsCategoryEs> resultList = result.stream()
                .map(goods -> {
                    DtsCategoryEs category = new DtsCategoryEs();
                    category.setId(goods.getCategoryId().toString());
                    category.setName(goods.getCategoryname());
                    return category;
                })
                .distinct() // 依赖 equals/hashCode
                .collect(Collectors.toList());
        System.out.println(resultList.size());
        if (!resultList.isEmpty()) {
            return success(resultList);
        } else {
            return error("未找到相关商品");
        }
    }


    @GetMapping("/categoryGoods")
    public AjaxResult categoryGoods()
    {
        startPage();
        List<DtsCategory> dtsCategoryList = dtscateDetailService.getCateDetail();
        List<DtsCategoryVo> dtsCategoryVoList = new ArrayList<>();
        for (DtsCategory dtsCategory: dtsCategoryList) {
            startPage();
            DtsCategoryVo dtsCategoryVo = dtsGoodsService.selectGoodsByCategoryVo(dtsCategory);
            dtsCategoryVoList.add(dtsCategoryVo);
        }
        if (dtsCategoryVoList != null){
            return success(dtsCategoryVoList);
        } else {
            return error();
        }
    }


}
